package com.futu.scenicspot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.futu.attraction.domain.City;
import com.futu.attraction.domain.CitySopt;
import com.futu.attraction.service.CityService;
import com.futu.attraction.service.CitySoptService;
import com.futu.common.vo.ResultVo;
import com.futu.scenicspot.domain.*;
import com.futu.scenicspot.mapper.*;
import com.futu.scenicspot.service.HScenicSpotService;
import com.futu.scenicspot.vo.SoptVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86177
* @description 针对表【h_scenic_spot(景点信息表)】的数据库操作Service实现
* @createDate 2025-06-10 17:37:11
*/
@Service
public class HScenicSpotServiceImpl extends ServiceImpl<HScenicSpotMapper, HScenicSpot>
    implements HScenicSpotService{
    @Resource
    private HScenicSpotMapper hScenicSpotMapper;
    @Resource
    private HPreparationMapper hPreparation;
    @Resource
    private HActivityMapper hActivity;

    @Resource
    private HNoticeMapper hNotice;
    @Resource
    private HScenicImageMapper hScenicImageMapper;

    @Resource
    private CityService cityService;
    @Resource
   private  CitySoptService  citySoptService;
    @Override
    public ResultVo selall(Long id) {
        System.out.println("开始查询景点详情，ID: " + id);
        try {
            SoptVo soptVo = new SoptVo();
            HScenicSpot hScenicSpot = hScenicSpotMapper.selectById(id);
            if (hScenicSpot == null) {
                System.err.println("景点不存在，ID: " + id);
                return ResultVo.error("景点不存在，ID: " + id);
            }
            System.out.println("找到景点: " + hScenicSpot.getName() + ", ID: " + hScenicSpot.getId());
            
            soptVo.setName(hScenicSpot.getName());
            soptVo.setDescription(hScenicSpot.getDescription());
            soptVo.setLatitude(hScenicSpot.getLatitude());
            soptVo.setLongitude(hScenicSpot.getLongitude());
            soptVo.setPanoramaUrl(hScenicSpot.getPanoramaUrl());
            soptVo.setCreateTime(hScenicSpot.getCreateTime());
            soptVo.setUpdateTime(hScenicSpot.getUpdateTime());

            // 查询行前准备信息
            QueryWrapper<HPreparation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("scenic_spot_id", hScenicSpot.getId());
            HPreparation hPreparation1 = hPreparation.selectOne(queryWrapper);
            if (hPreparation1 == null) {
                System.err.println("景点前行准备不存在，景点ID: " + id);
                // 不抛出异常，而是设置为null
                soptVo.setHPreparation(null);
            } else {
                soptVo.setHPreparation(hPreparation1);
            }
            
            // 查询景点活动
            QueryWrapper<HActivity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("scenic_spot_id", hScenicSpot.getId());
            HActivity hActivity1 = hActivity.selectOne(queryWrapper1);
            if (hActivity1 == null) {
                System.err.println("景点活动不存在，景点ID: " + id);
                // 不抛出异常，而是设置为null
                soptVo.setHActivity(null);
            } else {
                soptVo.setHActivity(hActivity1);
            }
            
            // 查询景点注意事项
            QueryWrapper<HNotice> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("scenic_spot_id", hScenicSpot.getId());
            HNotice hNotice1 = hNotice.selectOne(queryWrapper2);
            if (hNotice1 == null) {
                System.err.println("景点注意事项不存在，景点ID: " + id);
                // 不抛出异常，而是设置为null
                soptVo.setNotice(null);
            } else {
                soptVo.setNotice(hNotice1);
            }
            
            // 查询景点图片
            List<HScenicImage> imageList = hScenicImageMapper.findByScenicId(hScenicSpot.getId());
            if (imageList == null || imageList.isEmpty()) {
                System.err.println("景点图片不存在，景点ID: " + id);
                // 不抛出异常，使用默认图片
                soptVo.setImgUrl("https://example.com/default-image.jpg");
                soptVo.setImageUrls(new ArrayList<>());
            } else {
                // 设置第一张图片作为主图片
                soptVo.setImgUrl(imageList.get(0).getImageUrl());
                
                // 收集所有图片URL
                List<String> imageUrls = imageList.stream()
                    .map(HScenicImage::getImageUrl)
                    .collect(Collectors.toList());
                soptVo.setImageUrls(imageUrls);
            }
            
            // 查询城市信息
            try {
                CitySopt one = citySoptService.getOne(new LambdaQueryWrapper<CitySopt>().eq(CitySopt::getSoptId, id));
                if (one != null) {
                    City byId = cityService.getById(one.getCityId());
                    if (byId != null) {
                        soptVo.setCityName(byId.getCityName());
                    } else {
                        System.err.println("未找到城市信息，城市ID: " + one.getCityId());
                        soptVo.setCityName("未知城市");
                    }
                } else {
                    System.err.println("未找到景点-城市关联信息，景点ID: " + id);
                    soptVo.setCityName("未知城市");
                }
            } catch (Exception e) {
                System.err.println("查询城市信息失败: " + e.getMessage());
                soptVo.setCityName("未知城市");
            }
            
            System.out.println("景点详情查询成功，返回数据: " + soptVo);
            return ResultVo.success(soptVo);
        } catch (Exception e) {
            System.err.println("查询景点详情异常: " + e.getMessage());
            e.printStackTrace();
            return ResultVo.error("查询景点详情失败: " + e.getMessage());
        }
    }
}




